package cn.music.test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MusicTestActivity extends Activity {
	private Button music_btn = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		music_btn = (Button)this.findViewById(R.id.music_btn);
		copyFile();

	}

	@Override
	protected void onResume()   {
		super.onResume();

		
		music_btn.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {

		 
				String path = "file:///data/data/cn.music.test/files/boys.mp3";
		    	Intent intent = new Intent(Intent.ACTION_VIEW);  
		    	Uri uri = Uri.parse(path);
		        intent.setDataAndType(uri,"audio/MP3");
		        startActivity(intent); 
			}
			
		});
 
		
	}
	
	public void copyFile(){
		InputStream in = null;
		OutputStream out = null;
		 
		String path = "/data/data/cn.music.test/files/boys.mp3";
		
		try {
			this.openFileOutput("boys.mp3", this.MODE_WORLD_READABLE);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		try {
			
			in = getAssets().open("boys.mp3");
			out = new FileOutputStream(path);
			byte[] buff = new byte[1024];
			int len = 0;
			while ((len = in.read(buff))!= -1) {
				
				out.write(buff, 0, len);
				
			}
			out.flush();
			out.close();
			in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}